package org.example.system.controller.system;

import lombok.RequiredArgsConstructor;
import org.example.common.core.domain.R;
import org.example.common.core.utils.page.Pager;
import org.example.common.core.validate.AddGroup;
import org.example.common.core.validate.EditGroup;
import org.example.common.excel.utils.ExcelUtil;
import org.example.common.log.annotation.OperatorLog;
import org.example.common.log.enums.BusinessType;
import org.example.common.security.annotation.PreAuthorize;
import org.example.common.web.annotation.RepeatSubmit;
import org.example.system.domain.bo.SysClientBo;
import org.example.system.domain.vo.SysClientVo;
import org.example.system.service.ISysOauthClientService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 客户端管理
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/client")
public class SysClientController {

    private final ISysOauthClientService sysClientService;

    /**
     * 查询客户端管理列表
     */
    @PreAuthorize(hasAnyPermi = {"system:client:list"})
    @PostMapping("/list")
    public Pager<SysClientVo> list(@RequestBody Pager pager) {
        return sysClientService.selectOauthClientPage(pager);
    }

    /**
     * 导出客户端管理列表
     */
    @PreAuthorize(hasAnyPermi={"system:client:export"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody Pager pager, HttpServletResponse response) {
        List<SysClientVo> list = sysClientService.selectOauthClientList(pager);
        ExcelUtil.exportExcel(list, "客户端管理", SysClientVo.class, response);
    }

    /**
     * 获取客户端管理详细信息
     *
     * @param clientId 主键
     */
    @PreAuthorize(hasAnyPermi = {"system:client:query"})
    @GetMapping("/{clientId}")
    public R<SysClientVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable String clientId) {
        return R.ok(sysClientService.selectOauthClientByClientId(clientId));
    }

    /**
     * 预生成客户端
     */
    @PreAuthorize(hasAnyPermi = {"system:client:add"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/preBuild")
    public R<SysClientVo> preBuild() {
        return R.ok(sysClientService.preBuild());
    }

    /**
     * 新增客户端管理
     */
    @PreAuthorize(hasAnyPermi = {"system:client:add"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysClientBo bo) {
        return R.toAjax(sysClientService.createOauthClientDetails(bo));
    }

    /**
     * 修改客户端管理
     */
    @PreAuthorize(hasAnyPermi = {"system:client:edit"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysClientBo bo) {
        return R.toAjax(sysClientService.updateOauthClientDetails(bo));
    }

    /**
     * 重置密钥
     * @param clientId
     * @return
     */
    @PreAuthorize(hasAnyPermi = {"system:client:edit"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/resetClientSecret/{clientId}")
    public R resetClientSecret(@NotEmpty(message = "主键不能为空")
                            @PathVariable String clientId) {
        return R.ok(sysClientService.resetClientSecret(clientId));
    }

    /**
     * 删除客户端管理
     *
     * @param clientId 主键串
     */
    @PreAuthorize(hasAnyPermi = {"system:client:remove"})
    @OperatorLog(title = "客户端管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clientId}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String clientId) {
        return R.toAjax(sysClientService.deleteOauthClientDetails(clientId));
    }
}
